package com.geekaca;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.geekaca.pojo.Brand;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class TestPool {

    public static void main(String[] args) throws Exception {
        /**
         * 学习连接池
         */
        //1.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/main/resources/druid.properties"));
        //2, 构造连接池对象
        /**
         * DataSource 是Java官方的规范,数据库连接池规范
         */
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        // 从数据库连接池获取连接
        Connection conn = dataSource.getConnection();
        // 常规操作
        String sql = "select * from brands.tb_brand";

        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("resultSet: " + resultSet);
        List<Brand> brandList = new ArrayList<>();
//        System.out.println(brandList);

        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String brandName = resultSet.getString("brand_name");
            String companyName = resultSet.getString("company_name");
            int ordered = resultSet.getInt("ordered");
            String description = resultSet.getString("description");
            int status = resultSet.getInt("status");
            System.out.println(id + " " + brandName + " " + companyName);

            // 优为为用构造方法传值
            Brand brand = new Brand(id, brandName, companyName, ordered, description, status);
          /*  brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);*/
            brandList.add(brand);
        }
        for (int i = 0; i < brandList.size(); i++) {
            System.out.println(brandList.get(i));
        }

        // 增加数据
        /*String sql2 = "insert into tb_brand values(null,?,?,?,?,0)";
        String brandName ="vivo";
        String companyName=" vivo 智能手机制造公司 ";
        int ordered = 3;
        String description = " 最好用的音乐手机 ";

        PreparedStatement pstmt = conn.prepareStatement(sql2);
        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);*/

//        pstmt.executeUpdate();

        // 修改数据
        /*String sql3 = "update tb_brand set brand_name='五只松鼠' where id=1";
        Statement stmt1 = conn.createStatement();
        stmt1.executeUpdate(sql3);*/


        //  删除数据
        /*String sql4 = "delete from tb_brand where brand_name='vivo'";
        stmt1.executeUpdate(sql4);*/

        //归还连接到连接池
        conn.close();
    }

}
